#!/usr/bin/env python


from tzutil.retry import retry
from requests import Session as _Session
from proxy import PROXY, proxy_header


class Session(_Session):
    def __init__(self, *args, **kwds):
        self._default =  dict(
            timeout = 60,
            proxies=dict(http=PROXY,https=PROXY),
            verify=False,
        )
        super(Session, self).__init__(
            *args, **kwds
        )
        self.headers.update(
            proxy_header()
        )
        self.headers.update({
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
        })

    @retry
    def request(self, url, *args, **kwds):
        for k,v in self._default.items():
            if not k in kwds:
                kwds[k] = v
        kwds['allow_redirects']=False

        # if 'headers' in kwds:
        #     headers = kwds['headers']
        # else:
        #     headers = {}
        # kwds['headers'] = headers = {}
        # headers.update(self.headers)
        # headers.update(headers)
        # print(self._default)
        return super(Session,self).request(url, *args, **kwds)


